<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Verify and save data from customers-editorpage
 * used from customer.page.php
 * 
 * @author Norbert Gebert
 * @copyright Norbert Gebert 25.09.2011
 * @license GPL3
 *
 * @version 0.5b
 * @package Admin
 * @subpackage page
 */
$idx = $refValidate->testNumber($_POST['idxx'], _('Customer-ID'));
$mysqlBuffer = array();

// Verify all data and build query
if (!empty($_POST['companyx']))
{
	$datax = $refValidate->testText($_POST['companyx'], _('Company'));
	if ($datax)
		$mysqlBuffer['customer_company'] = $datax;
}

if (!empty($_POST['firstnamex']))
{
	$datax = $refValidate->testText($_POST['firstnamex'], _('Firstname'));
	if ($datax)
		$mysqlBuffer['customer_firstname'] = $datax;
}

if (!empty($_POST['lastnamex']))
{
	$datax = $refValidate->testText($_POST['lastnamex'], _('Lastname'));
	if ($datax)
		$mysqlBuffer['customer_lastname'] = $datax;
}

if (!empty($_POST['streetx']))
{
	$datax = $refValidate->testText($_POST['streetx'], _('Street'));
	if ($datax)
		$mysqlBuffer['customer_street'] = $datax;
}

if (!empty($_POST['zipx']))
{
	$datax = $refValidate->testNumber($_POST['zipx'], _('Zipcode'));
	if ($datax)
		$mysqlBuffer['customer_zip'] = $datax;
}

if (!empty($_POST['cityx']))
{
	$datax = $refValidate->testText($_POST['cityx'], _('City'));
	if ($datax)
		$mysqlBuffer['customer_city'] = $datax;
}

if (!empty($_POST['countryx']))
{	
	$datax = $refValidate->testText($_POST['countryx'], _('Country'));
	if ($datax)
		$mysqlBuffer['customer_country'] = $datax;
}

if (!empty($_POST['phonex']))
{
	$datax = $refValidate->testPhonenumber($_POST['phonex'], _('Phone'));
	if ($datax)
		$mysqlBuffer['customer_phone'] = $datax;
}

if (!empty($_POST['faxx']))
{
	$datax = $refValidate->testPhonenumber($_POST['faxx'], _('Fax'));
	if ($datax)
		$mysqlBuffer['customer_fax'] = $datax;
}

if (!empty($_POST['handyx']))
{
	$datax = $refValidate->testPhonenumber($_POST['handyx'], _('Handy'));
	if ($datax)
		$mysqlBuffer['customer_handy'] = $datax;
}

if (!empty($_POST['emailx']))
{
	$datax = $refValidate->testEmail($_POST['emailx'], _('Email'));
	if ($datax)
		$mysqlBuffer['customer_email'] = $datax;
}

if (!empty($_POST['levelx']))
{
	$_POST['levelx'] = round($_POST['levelx']);
	$datax = $refValidate->testNumber($_POST['levelx'], _('Level'));

	if ($datax)
		$mysqlBuffer['customer_level'] = $datax;
}

if (!empty($_POST['namex']))
{
	$datax = $refValidate->testText($_POST['namex'], _('Username'));
	db::getInstance()->where('customer_name', $datax);
	$result = db::getInstance()->getOne('customer');

	if (db::getInstance()->count >= 1 and $result['customer_id'] <> $idx)
	{
		message::setErrorRow(_('The username is already in use!') . '<br>' .
			_('The old user name is not changed.'), true);
	}
	else
	{
		if ($datax)
			$mysqlBuffer['customer_name'] = $datax;
	}
}
else
{
	message::setErrorRow(_('This Customer must have a Username!') . '<br>' .
		_('The name was set to the Standard = _Nobody_.'), true);
	$mysqlBuffer['customer_name'] = '_Nobody_';
	$_POST['namex'] = '_Nobody_';
}

if (!empty($_POST['textx']))
{
	$datax = $refValidate->testText($_POST['textx'], _('Info'));
	if ($datax)
		$mysqlBuffer['customer_info'] = $datax;
}

if (!empty($_POST['errorsx']))
{
	$datax = $refValidate->testNumber($_POST['errorsx'], _('Errors'));
	if ($datax)
		$mysqlBuffer['customer_errors'] = $datax;
}

if (isset($_POST['activex']) and $_POST['namex'] <> '_Nobody_')
{
	db::getInstance()->where('customer_id', $idx);
	$result = db::getInstance()->getOne('customer');

	if (!empty($result['customer_password']) and ! empty($result['customer_secureid']))
		$mysqlBuffer['customer_active'] = '1';
	else
	{
		message::setErrorRow(_('This Customer must have a password!') . '<br>' .
			_('You can activate the customer if it has a password.'), true);
		$mysqlBuffer['customer_active'] = '0';
	}
}
else
	$mysqlBuffer['customer_active'] = '0';

db::getInstance()->where('customer_id', $idx);
$result = db::getInstance()->getOne('customer');

if (db::getInstance()->count >= 1)
{
	db::getInstance()->where('customer_id', $idx);
	if (db::getInstance()->update('customer', $mysqlBuffer))
		message::setNoteRow(_('Data saved successfull!'), true);
	else
		message::setErrorRow(_('Data could not save!'), true);
}
else
	message::setErrorRow(_('Data could not save!') . ' -> ID not found', true);